package com.ruoyi.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;

import com.ruoyi.product.domain.WoOpLog;
import com.ruoyi.product.domain.WoRaw;

import com.ruoyi.product.domain.WoSubRaw;
import com.ruoyi.product.domain.WorkOrder;
import com.ruoyi.product.domain.dto.WoRawDto;
import com.ruoyi.product.domain.vo.WoRawVo;
import com.ruoyi.product.mapper.WoRawMapper;
import com.ruoyi.product.mapper.WoSubRawMapper;

import com.ruoyi.product.mapper.WorkOrderMapper;
import com.ruoyi.product.service.WoRawService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class WoRawServiceImpl implements WoRawService {
    @Resource
    WoRawMapper woRawMapper;

    @Resource
    WoSubRawMapper woRawRelMapper;

    @Resource
    WorkOrderMapper workOrderMapper;


    @Override
    public Page<WoRawVo> getWoRawByPage(WoRawDto pageEntity) {
        //1.如果不为空不查询


        //2.查询(不为NULL)
        List<Long> woRawIds = new ArrayList<>();
        if(null != pageEntity.getSubId()) {
            MPJLambdaWrapper<WoSubRaw> wrapper = new MPJLambdaWrapper<WoSubRaw>()
                    .selectAll(WoSubRaw.class)
                    .eq(WoSubRaw::getDelFlag, 0)
                    .eq(WoSubRaw::getWoRawSubId, pageEntity.getSubId());
            List<WoSubRaw> woRawRels = woRawRelMapper.selectJoinList(wrapper);
            woRawIds = woRawRels.stream().map(item -> item.getWoRawId()).distinct().collect(Collectors.toList());
        }

        MPJLambdaWrapper<WoRaw> wrapper = new MPJLambdaWrapper<WoRaw>()
                .selectAll(WoRaw.class)//查询user表全部字段
                //记的测试阶段
                .eq(WoRaw::getDelFlag, 0)
                .eq(Objects.nonNull(pageEntity.getWo()), WoRaw::getWo, pageEntity.getWo())
                .in(woRawIds != null && woRawIds.size() >0, WoRaw::getId, woRawIds)
                .orderByDesc(WoRaw::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, WoRaw.class, pageEntity);
        Page<WoRawVo> userList = woRawMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   WoRawVo.class, wrapper);
        return userList;
    }

    @Override
    public void addWoRaw(WoRawDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<WoRaw> wrapper = new MPJLambdaWrapper<WoRaw>()
                .eq(WoRaw::getRawCode, testDto.getRawCode())
                .eq(WoRaw::getWo,testDto.getWo())
                .ne(WoRaw::getDelFlag, 1);
        WoRaw material = woRawMapper.selectOne(wrapper);
        if(null != material) {
            throw new GlobalException("该material已经存在");
        }
        MPJLambdaWrapper<WorkOrder> wrapper2 = new MPJLambdaWrapper<WorkOrder>()
                .eq(WorkOrder::getWo, testDto.getWo())
                .ne(WorkOrder::getDelFlag, 1);
        WorkOrder workOrder = workOrderMapper.selectOne(wrapper2);
        if("涂布".equals(workOrder.getType()) && (workOrder.getProceduresNum() == 1 || workOrder.getProceduresNum() == 2)) {
            throw new GlobalException("涂布前两道工序不允许录入原材料");
        }
        WoRaw test2 = new WoRaw();

        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
        test2.setStatus("未消耗");
        test2.setRawType("原材料");
        test2.setDelFlag(false);
        test2.setCreateBy(userName);
        test2.setCreateTime(new Date());
        woRawMapper.insert(test2);
    }

    @Override
    public void addWoRaws(List<WoRawDto> woRawDtos) {
        String userName = SecurityUtils.getUsername();
        List<String> rowCodes = woRawDtos.stream()
                .map(item -> item.getRawCode()).distinct().collect(Collectors.toList());
        MPJLambdaWrapper<WoRaw> wrapper = new MPJLambdaWrapper<WoRaw>()
                .in(WoRaw::getRawCode, rowCodes)
                .eq(WoRaw::getWo,woRawDtos.get(0).getWo())
                .ne(WoRaw::getDelFlag, 1);
        List<WoRaw> material = woRawMapper.selectList(wrapper);
        if(null != material && material.size() > 0) {
            throw new GlobalException("该工单下的物料号已经存在");
        }
        /**
         * 代码修改提交
         */
        MPJLambdaWrapper<WorkOrder> wrapper2 = new MPJLambdaWrapper<WorkOrder>()
                .eq(WorkOrder::getWo, woRawDtos.get(0).getWo())
                .ne(WorkOrder::getDelFlag, 1);
        WorkOrder workOrder = workOrderMapper.selectOne(wrapper2);
        if("涂布".equals(workOrder.getType()) && (workOrder.getProceduresNum() == 1 || workOrder.getProceduresNum() == 2)) {
            throw new GlobalException("涂布前两道工序不允许录入原材料");
        }
        for(WoRawDto woRawDto : woRawDtos ) {
            WoRaw test2 = new WoRaw();
            BeanUtils.copyProperties(woRawDto, test2, new String[]{"id"});
            test2.setStatus("未消耗");
            test2.setRawType("原材料");
            test2.setDelFlag(false);
            test2.setCreateBy(userName);
            test2.setCreateTime(new Date());
            woRawMapper.insert(test2);
        }
    }

    @Override
    public void updateWoRaw(WoRawDto testDto) {
        MPJLambdaWrapper<WoRaw> wrapper = new MPJLambdaWrapper<WoRaw>()
                .eq(WoRaw::getRawCode, testDto.getRawCode());
        WoRaw material = woRawMapper.selectOne(wrapper);
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        BeanUtils.copyProperties(testDto, material);
        material.setCreateBy("admin");
        material.setCreateTime(new Date());
        woRawMapper.updateById(material);
    }

    @Override
    public void delWoRaw(WoRawDto testDto) {
        WoRaw material = woRawMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        material.setDelFlag(true);
        woRawMapper.updateById(material);
    }

    @Override
    public void updateWoRawStatus(WoRawDto testDto) {
        WoRaw woRaw = woRawMapper.selectById(testDto.getId());
        if(null == woRaw) {
            throw new GlobalException("原材料不存在");
        }
        String userName = SecurityUtils.getUsername();
        woRaw.setStatus(testDto.getStatus());
        woRaw.setUpdateTime(new Date());
        woRaw.setUpdateBy(userName);
        woRawMapper.updateById(woRaw);
    }

    @Override
    public void updateWoRawActualLossValue(List<WoRawDto> woRawDtos) {
        String userName = SecurityUtils.getUsername();
        for(WoRawDto woRawDto: woRawDtos) {
            WoRaw woRaw = woRawMapper.selectById(woRawDto.getId());
            woRaw.setActualLossValue(woRawDto.getActualLossValue());
            woRaw.setUpdateTime(new Date());
            woRaw.setUpdateBy(userName);
            woRawMapper.updateById(woRaw);
        }
    }
}




